package com.sitech.portal.webHooks;

import com.sitech.ibnms.core.BaseIbnmsAction;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.system.domain.TblSYS_WORKGROUP;
import com.sitech.portal.system.service.ISysWorkGroupService;
import com.sitech.portal.webHooks.domain.HookRequestBody;
import com.sitech.portal.webHooks.domain.WebHooks;
import com.sitech.portal.webHooks.domain.WorkGroup;
import com.sitech.portal.webHooks.service.IWebHooksService;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: yanran
 * Date: 15-9-11
 * Time: 上午10:19
 * To change this template use File | Settings | File Templates.
 */
public class WorkGroupEventProcessor extends BaseIbnmsAction {
    private IWebHooksService webHooksService;
    private ISysWorkGroupService sysWorkGroupService;
    private static final Log logger = LogFactory.getLog(WorkGroupEventProcessor.class);


    public Object whenAddWorkGroup(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        TblSYS_WORKGROUP wg= (TblSYS_WORKGROUP)joinPoint.getArgs()[0];
        WorkGroup afterWG = new WorkGroup(wg);
        afterWG.setCREATED_BY(this.getCurrentUser().getLOGIN_ID());
        afterWG.setCREATED_AT(EventProcessorUtil.getNowTime());
        WorkGroup beforeWG = null;

        String object_id = wg.getWORKGROUP_ID();
        String object_kind = "workgroup";
        String operate_kind = "create";
        Object o = null;
        try {
            o = joinPoint.proceed();
            //调用接口
            List<WebHooks>  list= webHooksService.selectWebHooksUrl(object_kind);
            HookRequestBody hrb = new HookRequestBody();
            hrb.setObject_kind(object_kind);
            hrb.setOperate_kind(operate_kind);
            hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(beforeWG));
            hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(afterWG));
            hrb.setObject_id(object_id);

            for(WebHooks os : list){
                String url = os.getURL();
                PostMethod post = new PostMethod(url);
                 post.setRequestBody(JSONUtil.toJSON(hrb));
                try {
                    new HttpClient().executeMethod(post);
                }catch (Exception e){
                    logger.error(e);
                }
                logger.info("增加工作组同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }
    public Object whenUpdateWorkGroup(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        TblSYS_WORKGROUP wg= (TblSYS_WORKGROUP)joinPoint.getArgs()[0];
        TblSYS_WORKGROUP before =  sysWorkGroupService.findWorkGroupById(wg.getWORKGROUP_ID());

        WorkGroup afterWG = new WorkGroup(wg);
        afterWG.setUPDATED_BY(this.getCurrentUser().getLOGIN_ID());
        afterWG.setUPDATED_AT(EventProcessorUtil.getNowTime());
        WorkGroup beforeWG = new WorkGroup(before);

        String object_id = before.getWORKGROUP_ID();
        String object_kind = "workgroup";
        String operate_kind = "update";
        Object o = null;
        try {
            o = joinPoint.proceed();
            //调用接口
            List<WebHooks>  list= webHooksService.selectWebHooksUrl(object_kind);
            HookRequestBody hrb = new HookRequestBody();
            hrb.setObject_kind(object_kind);
            hrb.setOperate_kind(operate_kind);
            hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(beforeWG));
            hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(afterWG));
            hrb.setObject_id(object_id);

            for(WebHooks os : list){
                String url = os.getURL();
                PostMethod post = new PostMethod(url);
                post.setRequestBody(JSONUtil.toJSON(hrb));
                try {
                    new HttpClient().executeMethod(post);
                }catch (Exception e){
                    logger.error(e);
                }
                logger.info("修改工作组同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }

    public Object whenDeleteWorkGroup(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

        String wgId = (String)joinPoint.getArgs()[0];
        String object_id = wgId;
        String object_kind = "workgroup";
        String operate_kind = "delete";

        TblSYS_WORKGROUP before =  sysWorkGroupService.findWorkGroupById(wgId);
        WorkGroup beforeWG = new WorkGroup(before);
        WorkGroup afterWG = new WorkGroup();
        afterWG.setWORKGROUP_ID(object_id);
        afterWG.setDELETED_BY(this.getCurrentUser().getLOGIN_ID());
        afterWG.setDELETED_AT(EventProcessorUtil.getNowTime());

        Object o = null;
        try {
            o = joinPoint.proceed();
            //调用接口
            List<WebHooks>  list= webHooksService.selectWebHooksUrl(object_kind);
            HookRequestBody hrb = new HookRequestBody();
            hrb.setObject_kind(object_kind);
            hrb.setOperate_kind(operate_kind);
            hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(beforeWG));
            hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(afterWG));
            hrb.setObject_id(object_id);

            for(WebHooks os : list){
                String url = os.getURL();
                PostMethod post = new PostMethod(url);
                post.setRequestBody(JSONUtil.toJSON(hrb));
                try {
                    new HttpClient().executeMethod(post);
                }catch (Exception e){
                    logger.error(e);
                }
                logger.info("删除工作组同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }

    public void setWebHooksService(IWebHooksService webHooksService) {
        this.webHooksService = webHooksService;
    }

    public void setSysWorkGroupService(ISysWorkGroupService sysWorkGroupService) {
        this.sysWorkGroupService = sysWorkGroupService;
    }
}
